from flask import Blueprint, jsonify, request
from application.extensions.logging import system_logger
from application.services.system.user import UserService
from application.utils.res import json_fail, json_success

# 创建蓝图
bp = Blueprint("user", __name__, url_prefix="/user")


@bp.get("/getInfo")
def get_User_Info():
    """
    获取用户信息接口
    返回用户的基本信息、角色和按钮权限
    """
    response_data = {
        "code": 200,
        "msg": "操作成功",
        "data": {
            "user": {
                "userId": 1,
                "tenantId": "000000",
                "deptId": 103,
                "userName": "admin",
                "nickName": "疯狂的狮子Li",
                "userType": "sys_user",
                "email": "crazyLionLi@163.com",
                "phonenumber": "15888888888",
                "sex": "1",
                "avatar": "https://minio.xlsea.cn/ruoyi/2025/06/03/b15d9d0301b845c3aa42840f58d93000.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20250910T060739Z&X-Amz-SignedHeaders=host&X-Amz-Credential=LWEr34D1C0CGKkAwKw7h%2F20250910%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Expires=120&X-Amz-Signature=35e2c93eaa0fef5c4e570ec8022adc7878795e92b7f3a671d52cb1478ed1ac64",
                "status": "0",
                "loginIp": "122.246.30.184",
                "loginDate": "2025-09-10 14:07:39",
                "remark": "管理员",
                "createTime": "2025-05-21 02:26:43",
                "deptName": "研发部门",
                "roles": [
                    {
                        "roleId": 1,
                        "roleName": "超级管理员",
                        "roleKey": "superadmin",
                        "roleSort": 1,
                        "dataScope": "1",
                        "menuCheckStrictly": None,
                        "deptCheckStrictly": None,
                        "status": "0",
                        "remark": None,
                        "createTime": None,
                        "flag": False,
                        "superAdmin": True,
                    }
                ],
                "roleIds": None,
                "postIds": None,
                "roleId": None,
            },
            "permissions": ["*:*:*"],
            "roles": ["superadmin"],
        },
    }

    return jsonify(response_data)


@bp.get("/list")
def get_User_List():
    """
    分页获取用户列表接口
    支持条件查询：pageNum, pageSize, userName, nickName, phonenumber, status, beginTime, endTime
    """
    try:
        # 获取分页参数
        page_num = int(request.args.get("pageNum", 1))
        page_size = int(request.args.get("pageSize", 10))

        # 获取查询条件
        conditions = {
            "userName": request.args.get("userName"),
            "nickName": request.args.get("nickName"),
            "phonenumber": request.args.get("phonenumber"),
            "status": request.args.get("status"),
            "params": {
                "beginTime": request.args.get("params[beginTime]"),
                "endTime": request.args.get("params[endTime]"),
            },
        }

        # 过滤空值条件
        filtered_conditions = {}
        for k, v in conditions.items():
            if k == "params":
                param_dict = {pk: pv for pk, pv in v.items() if pv}
                if param_dict:
                    filtered_conditions[k] = param_dict
            elif v is not None and v != "":
                filtered_conditions[k] = v

        # 调用服务获取数据
        result = UserService.get_user_page(page_num, page_size, filtered_conditions)

        return json_success(result)

    except Exception as e:

        return json_fail(f"查询失败: {str(e)}"), 500
   

@bp.get("/deptTree")
def get_Dept_Tree():
    """
    获取用户信息接口
    返回用户的基本信息、角色和按钮权限
    """
    response_data = {
        "code": 200,
        "msg": "操作成功",
        "data": [
            {
                "id": 100,
                "parentId": 0,
                "label": "XXX科技",
                "weight": 0,
                "disabled": False,
                "children": [
                    {
                        "id": 101,
                        "parentId": 100,
                        "label": "深圳总公司",
                        "weight": 1,
                        "disabled": False,
                        "children": [
                            {
                                "id": 103,
                                "parentId": 101,
                                "label": "研发部门",
                                "weight": 1,
                                "disabled": False,
                            },
                            {
                                "id": 104,
                                "parentId": 101,
                                "label": "市场部门",
                                "weight": 2,
                                "disabled": False,
                            },
                            {
                                "id": 105,
                                "parentId": 101,
                                "label": "测试部门",
                                "weight": 3,
                                "disabled": False,
                            },
                            {
                                "id": 106,
                                "parentId": 101,
                                "label": "财务部门",
                                "weight": 4,
                                "disabled": False,
                            },
                            {
                                "id": 107,
                                "parentId": 101,
                                "label": "运维部门",
                                "weight": 5,
                                "disabled": False,
                            },
                            {
                                "id": "1958057020350279682",
                                "parentId": 101,
                                "label": "运维部",
                                "weight": 5,
                                "disabled": False,
                            },
                        ],
                    },
                    {
                        "id": 102,
                        "parentId": 100,
                        "label": "长沙分公司",
                        "weight": 2,
                        "disabled": False,
                        "children": [
                            {
                                "id": 108,
                                "parentId": 102,
                                "label": "市场部门",
                                "weight": 1,
                                "disabled": False,
                            },
                            {
                                "id": 109,
                                "parentId": 102,
                                "label": "财务部门",
                                "weight": 2,
                                "disabled": False,
                            },
                        ],
                    },
                ],
            }
        ],
    }
    return jsonify(response_data)
